package com.zhiqian.community.controller;

import com.zhiqian.community.entity.User;
import com.zhiqian.community.service.DataService;
import com.zhiqian.community.service.UserService;
import com.zhiqian.community.util.CommunityConstant;
import com.zhiqian.community.util.CommunityUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理接口
 */
@Controller
@RequestMapping("/admin")
public class DataController implements CommunityConstant {
    @Autowired
    private UserService userService;
    @Autowired
    private DataService dataService;

    @GetMapping("/index")
    public String adminIndex(Model model){
        List<User> users = userService.getAdminUsers();
        model.addAttribute("users",users);
        return "site/admin/index";
    }

    @PostMapping("/setAdmin/{username}")
    @ResponseBody
    public String setAdminUser(@PathVariable("username")String username){
        if (StringUtils.isBlank(username)){
            return CommunityUtil.getJSONString(1,"用户名不能为空");
        }
        User user = userService.getUserByName(username);
        if (user==null){
            return CommunityUtil.getJSONString(1,"该用户不存在");
        }
        if (user.getType().equals(AUTHORITY_ADMIN_CODE)){
            return CommunityUtil.getJSONString(1,"该用户已经是管理员，请不要重复设置");
        }
        userService.setUserType(user.getId(),AUTHORITY_ADMIN_CODE);
        return CommunityUtil.getJSONString(0,"设置成功");
    }

    @PostMapping("/cancelAdmin/{id}")
    @ResponseBody
    public String cancelAdminUser(@PathVariable("id")Integer id){
        User user = userService.getUserById(id);
        if (user==null){
            return CommunityUtil.getJSONString(1,"该用户不存在");
        }
        userService.setUserType(user.getId(),AUTHORITY_USER_CODE);
        return CommunityUtil.getJSONString(0,"操作成功");
    }


    @GetMapping("/data")
    public String getDataPage(){
        return "/site/admin/data";
    }

    /**
     * 获取某一时间段的UV数据
     * @param startDate
     * @param endDate
     * @return
     */
    @PostMapping("/uv")
    @ResponseBody
    public String getDUData(@DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
                            @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate){
        String timeCheck = timeCheck(startDate, endDate);
        if (timeCheck !=null){
            return timeCheck;
        }
        long uvCount = dataService.getUVCount(startDate, endDate);
        Map<String, Object> map = new HashMap<>(1);
        map.put("uvCount",uvCount);
        return CommunityUtil.getJSONString(0,map);
    }

    /**
     * 获取某一时间段的DAU数据
     * @param startDate
     * @param endDate
     * @return
     */
    @PostMapping("/dau")
    @ResponseBody
    public String getDauData(@DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
                             @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate){
        String timeCheck = timeCheck(startDate, endDate);
        if (timeCheck !=null){
            return timeCheck;
        }
        long dauCount = dataService.getDAUCount(startDate, endDate);
        Map<String, Object> map = new HashMap<>(1);
        map.put("dauCount",dauCount);
        return CommunityUtil.getJSONString(0,map);
    }

    /**
     * 时间参数校验
     * @param startDate
     * @param endDate
     * @return
     */
    private String timeCheck(Date startDate,Date endDate){
        if (startDate ==null || endDate ==null){
            return CommunityUtil.getJSONString(1,"日期不能为空");
        }
        if (startDate.compareTo(endDate)==1 || startDate.after(new Date()) || endDate.after(new Date())){
            return CommunityUtil.getJSONString(1,"请选择正确的日期");
        }
        return null;
    }
}
